package ahblue.cn.tanzi.util;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {

    public static String getStandardDate(long publish) {
        String temp = "";
        try {
            long now = System.currentTimeMillis() / 1000;
            long diff = now - publish;

            long years = diff / (60 * 60 * 24 * 30 * 12);
            long months = diff / (60 * 60 * 24 * 30);
            long days = diff / (60 * 60 * 24);
            long hours = (diff - days * (60 * 60 * 24)) / (60 * 60);
            long minutes = (diff - days * (60 * 60 * 24) - hours * (60 * 60)) / 60;

            if (years > 0) {
                temp = years + "年前";
            } else if (months > 0) {
                temp = months + "月前";
            } else if (days > 0) {
                temp = days + "天前";
            } else if (hours > 0) {
                temp = hours + "小时前";
            } else {
                temp = minutes + "分钟前";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return temp;
    }

    //十位时间戳字符串转小时分钟秒
    public static String getStandardDate(String timeStr) {
        try {
            long publish = Long.parseLong(timeStr);
            return getStandardDate(publish);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }


    public static String Hourmin(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("HH:mm:ss");
        @SuppressWarnings("unused") long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;
    } //十位时间戳字符串转年月

    public static String YearMon(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月");
        @SuppressWarnings("unused") long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    } //十位时间戳字符串转月日

    public static String MonthDay(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("MM月dd日");
        @SuppressWarnings("unused") long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;

    } //获取13位字符串格式的时间戳

    public static String getTime13() {
        long time = System.currentTimeMillis();

        String str13 = String.valueOf(time);

        return str13;

    } //获取10位字符串格式的时间戳

    public static String getTime() {
        long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳

        String str = String.valueOf(time);

        return str;

    }

    public static String YMDHMS(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
        @SuppressWarnings("unused") long lcc = Long.valueOf(time);
        int i = Integer.parseInt(time);
        String times = sdr.format(new Date(i * 1000L));
        return times;
    }

    /**
     * Get the time string by timestamp(second)
     *
     * @param time   the timestamp
     * @param format the format string like yyyy-MM-dd
     * @return the time string like 2016-12-17
     */
    public static String formatTime(int time, String format) {
        return formatTime(time * 1000L, format);
    }

    /**
     * Get the time sting by timestamp(milliseconds)
     *
     * @param time   the timestamp
     * @param format the format string like yyyy-MM-dd
     * @return the time string like 2016-12-17
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatTime(Long time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time));
    }

    /**
     * get the time stamp by time string
     *
     * @param time   the time string like 2016-12-17
     * @param format the format string like yyyy-MM-dd
     * @return milliseconds
     */
    @SuppressLint("SimpleDateFormat")
    public static long getMillisSecond(String time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }
}